package my.andoird.util.r1;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;

import android.util.Log;

public final class AndroidNetwork extends java.lang.Object
{
	public static final String DEBUG_TAG = "AndroidNetwork";
	
	private HTTP http;
	
	public AndroidNetwork( String url )
	{
		super();
		
		this.http = new HTTP( url );
		
		http.connect();
	}
	
	public InputStream getStream()
	{
		return null;
	}

	public InputStream getHttpContentStream()
	{
		InputStream is = null;

		if( this.http != null )
		{
			is = http.getData();
		}

		return is;
	}
	
	public int getSizeOfHTTPContent()
	{
		int size = 0;
		
		if( this.http != null )
		{
			size = http.getSize();
		}
		
		return size;
	}
	
	public void disconnectHTTP()
	{
		if( http != null)
		{
			this.http.disconnect();
		}
	}
	
	public static String getLocalIpAddress() 
	{
		try {

			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) 
			{
				NetworkInterface ni = en.nextElement();

				for (Enumeration<InetAddress> enumIpAddr = ni.getInetAddresses(); enumIpAddr.hasMoreElements();) 
				{
					InetAddress inetAddress = enumIpAddr.nextElement();

					if (!inetAddress.isLoopbackAddress()) 
					{
						return inetAddress.getHostAddress();
					}
				}
			}
		} 
		catch (SocketException ex) 
		{
			Log.e(DEBUG_TAG, ex.toString());
		}

		return null;
	}
}

class HTTP extends java.lang.Object 
{
	public static final String DEBUG_TAG ="HTTP";
	
	private InputStream is;
	
	private int size;
	
	private HttpURLConnection httpConn;
	
	private String url;
	
	public HTTP( String url ) 
	{
		super();
		
		this.size = 0 ;
		this.is = null;
		this.httpConn = null;
		this.url = url;
	}

	public boolean connect()
	{
		URL url = null;
		URLConnection conn = null;
		
		try 
		{
			String new_url = this.url.replace("#", "%23");
			
			Log.d(DEBUG_TAG, new_url);
			
			url = new URL( new_url );
			
			conn = url.openConnection();
			
			this.httpConn = (HttpURLConnection) conn;
			
//			this.httpConn.setAllowUserInteraction(false);
//			this.httpConn.setInstanceFollowRedirects(true);
//			this.httpConn.setRequestMethod("GET");
//			this.httpConn.connect(); 

//			if((this.httpConn.getResponseCode() == HttpURLConnection.HTTP_OK))
//			{
//				this.httpConn.setDoInput( true );
//				this.httpConn.connect(); 
//			}

		}
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
			Log.e( DEBUG_TAG, e.getMessage() ) ;
			return false;
		} 
		catch ( IOException e ) 
		{
			e.printStackTrace();
			Log.e( DEBUG_TAG, e.getMessage() );
			return false;
		}
		catch ( Exception ex )
		{
			ex.printStackTrace();
			Log.e( DEBUG_TAG, ex.getMessage() );
			return false;
		}
		
		return true;
	
	}
	
	public InputStream getData()
	{
		if( this.httpConn == null )
		{
			throw new NullPointerException();
		}
		
		int response = -1;
		
		try 
		{
			response = this.httpConn.getResponseCode();   
			
			if ( response == HttpURLConnection.HTTP_OK ) 
			{
				this.size = this.httpConn.getContentLength();
				this.is = this.httpConn.getInputStream();
			}  
		} 
		catch ( IOException e ) 
		{
			e.printStackTrace();
			Log.e(DEBUG_TAG, e.getMessage() );
			
			return null;
		}
		
		return is;
	}
	
	public void disconnect()
	{
//		try 
//		{
//			if( this.is != null )
//				this.is.close();
			
			if( this.httpConn != null )
			{
				this.httpConn.disconnect();
			}
			
			Log.d(DEBUG_TAG, "success to disconnect hppt connection");
			
//		} 
//		catch ( IOException e ) 
//		{
//			Log.e(DEBUG_TAG, "failed to disconnect hppt connection");
//		}

	}
	
	public int getSize()
	{
		return this.size; 
	}

}
